// Variables
float4x4 WorldViewProjectionMatrix;    //!< World Matrix
sampler TextureSampler : register(s0); //!< The texture

///
/// @brief Vertex Shader Input Struct
/// 
struct VertexShaderInput
{
    float4 Position  : POSITION0;
	float4 Color     : COLOR0;
	float2 TexCoords : TEXCOORD0;
};

///
/// @brief Vertex Shader Output  Struct
/// 
struct VertexShaderOutput
{
    float4 Position  : POSITION0;
	float4 Color     : COLOR0;
	float2 TexCoords : TEXCOORD0;
};

///
/// @brief Simple bypassing vertex shader.
///
VertexShaderOutput DelinearizeVertexShader(VertexShaderInput input)
{
    VertexShaderOutput output;

    // Set the position
    output.Position  = mul(input.Position, WorldViewProjectionMatrix);
	output.Color     = input.Color;
	output.TexCoords = input.TexCoords;

    // Return the value.
    return output;
}


///
/// @brief Linearizing Pixel Shader
///
float4 DelinearizePixelShader(VertexShaderOutput input) : COLOR0
{
	// Calculate the offset from center.
	float2 Offset    = float2(input.TexCoords.x * 2 - 1, input.TexCoords.y * 2 - 1);
	float2 AbsOffset = abs(Offset);
	float distance = min(length(Offset), 1);

	// Return alpha acoording to the
	float alpha;
	if (AbsOffset.x >= AbsOffset.y)
	{
		float2 NewCoords = float2(input.TexCoords.x, (Offset.y / AbsOffset.x + 1) / 2);
		alpha = (1 - tex2D(TextureSampler, NewCoords).r) * (1 - distance);
	}
	else
	{
		float2 NewCoords = float2(input.TexCoords.y, (Offset.x / AbsOffset.y + 1) / 2);
		alpha = (1 - tex2D(TextureSampler, NewCoords.xy).g) * (1 - distance);
	}
	return float4(input.Color.rgb, alpha);
	
}

technique Delinearize
{
    pass Pass1
    {
        VertexShader = compile vs_2_0 DelinearizeVertexShader();
        PixelShader  = compile ps_2_0 DelinearizePixelShader();
    }
}
